import time
from debater import Debater
from judge import Judge


class DebatePlatform:
    def __init__(self):
        self.pro_side_debaters = []
        self.con_side_debaters = []
        self.judge = None
        self.debate_history = []
        self.is_recording = False
        self.recorded_audio = []

    def add_pro_debater(self, debater):
        if isinstance(debater, Debater):
            self.pro_side_debaters.append(debater)
        else:
            raise ValueError("需传入 Debater 类型对象")

    def add_con_debater(self, debater):
        if isinstance(debater, Debater):
            self.con_side_debaters.append(debater)
        else:
            raise ValueError("需传入 Debater 类型对象")

    def set_judge(self, judge):
        if isinstance(judge, Judge):
            self.judge = judge
        else:
            raise ValueError("需传入 Judge 类型对象")

    def start_debate(self):
        print("欢迎来到本次辩论赛！")
        self._do_opening_statements()
        for round_num in range(1, 4):  # 假设共三轮辩论
            print(f"现在开始第 {round_num} 轮辩论：")
            self._run_round(round_num)
        print("辩论结束，等待裁判评判结果...")
        result = self.judge.judge_result(self.pro_side_debaters[0], self.con_side_debaters[0])
        print(f"裁判宣布：{result}")

    def _do_opening_statements(self):
        print("正方一辩开场：")
        self.pro_side_debaters[0].generate_debate_view("开场陈述")
        print("反方一辩开场：")
        self.con_side_debaters[0].generate_debate_view("开场陈述")

    def _run_round(self, round_num):
        for position in range(len(self.pro_side_debaters)):
            pro_debater = self.pro_side_debaters[position]
            con_debater = self.con_side_debaters[position]
            self._start_timer()
            print(f"正方{position + 1}辩发言：")
            view = pro_debater.generate_debate_view(f"第 {round_num} 轮辩论")
            self._stop_timer()
            self._record(view)
            self._start_timer()
            print(f"反方{position + 1}辩发言：")
            view = con_debater.generate_debate_view(f"第 {round_num} 轮辩论")
            self._stop_timer()
            self._record(view)

    def _start_timer(self):
        print("开始计时...")
        self.start_time = time.time()

    def _stop_timer(self):
        elapsed_time = time.time() - self.start_time
        print(f"用时：{elapsed_time} 秒")

    def _record(self, text):
        if self.is_recording:
            self.recorded_audio.append(text)
            self.debate_history.append(text)

    def start_recording(self):
        self.is_recording = True

    def stop_recording(self):
        self.is_recording = False

    def transcribe_audio(self):
        # 这里需使用真实的音频转录库，此处为模拟
        return [f"转录文本：{text}" for text in self.recorded_audio]


if __name__ == '__main__':
    # 创建辩手和裁判实例
    debater1_pro = Debater("正方观点提示词", 1, "")
    debater2_pro = Debater("正方观点提示词", 2, "")
    debater1_con = Debater("反方观点提示词", 1, "")
    debater2_con = Debater("反方观点提示词", 2, "")
    judge = Judge("简单辩论规则：论点清晰、逻辑连贯者胜")

    # 创建辩论赛平台实例并添加辩手和裁判
    platform = DebatePlatform()
    platform.add_pro_debater(debater1_pro)
    platform.add_pro_debater(debater2_pro)
    platform.add_con_debater(debater1_con)
    platform.add_con_debater(debater2_con)
    platform.set_judge(judge)

    # 启动辩论赛
    platform.start_debate()